package com.ftg.learn.chapter03;

import com.ftg.learn.chapter02.Student;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author kangjx
 * @createTime 2022/6/29
 */
public class PlayComparator {

    public static void main(String[] args) {
        //硬盘
        Elephant[] es = new Elephant[3];

        Elephant e1 = new Elephant();
        e1.setName("曼谷小象");
        e1.setWeight(20);

        Elephant e2 = new Elephant();
        e2.setName("曼谷大象");
        e2.setWeight(23);

        Elephant e3 = new Elephant();
        e3.setName("曼谷中象");
        e3.setWeight(13);


        es[0] = e1;
        es[1] = e2;
        es[2] = e3;

        //------》

//        for(int i = 0;i < es.length;i++){
//        for(int i = 0,len = es.length;i <len;i++){
//            for(int j = 0;j < len- i - 1;j++){
//                if(es[j].getWeight() > es[j+1].getWeight()){
//                    Elephant temp = es[j];
//                    es[j] = es[j+1];
//                    es[j+1] = temp;
//                }
//            }
//        }

        System.out.println(Arrays.toString(es));
        //比较器
        Arrays.sort(es, new Comparator<Elephant>() {
            @Override
            public int compare(Elephant o1, Elephant o2) {
                return -(o1.getWeight() - o2.getWeight());
            }
        });

        //语法糖
//        ()->{}
//        ()->return 2+3;
//        x->{}
//        (x,y)->{}
//        (x)->x.getAge(); --> Student::getAge
        Arrays.sort(es,(x,y)-> -(x.getWeight() - y.getWeight()));
//        Arrays.sort(es, Comparator.comparingInt(Elephant::getWeight));

        System.out.println(Arrays.toString(es));
        //hashcode

        System.out.println(e1.hashCode());
        System.out.println("456".hashCode());
    //Hash算法可以将一个数据转换为一个标志，这个标志和源数据的每一个字节都有十分紧密的关系。Hash算法还具有一个特点，就是很难找到逆向规律。
    }

}
